package com.yiwei.ydd.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.yiwei.ydd.R;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.InviteCheckBean;
import com.yiwei.ydd.api.bean.SetInviteBean;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.SureRefereeDilaog;
import com.yiwei.ydd.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MySetRefereeActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;

    private Unbinder unbiner;
    private SureRefereeDilaog sureRefereeDilaog;
    private String invite_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_referee_set);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("我的推荐人");
        sureRefereeDilaog = new SureRefereeDilaog(this);
        sureRefereeDilaog.setOnChooseListener(new SureRefereeDilaog.OnChooseListener() {
            @Override
            public void onSure() {
                getSetInvite(invite_code);
            }
        });
    }

    public void getInviteCheck(String keyword) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        InviteCheckBean bean = new InviteCheckBean();
        bean.keyword = keyword;

        Api.api_service.getInviteCheck(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (TextUtils.isEmpty(result.datas.invite_code)) {
                        ToastUtil.makeText(this,"该用户未生成邀请码");
                        return;
                    }
                    invite_code = result.datas.invite_code;
                    sureRefereeDilaog.setInviteData(UI.toString(editName));
                    sureRefereeDilaog.show();
                });
    }

    public void getSetInvite(String code) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        SetInviteBean bean = new SetInviteBean();
        bean.invite_code = code;
        Api.api_service.getSetInvite(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "修改成功");
                    Util.startActivity(this, MyRefereeActivity.class);
                    finish();
                });
    }


    @OnTextChanged(R.id.edit_name)
    void afterTextChanged(Editable s) {
        if (TextUtils.isEmpty(UI.toString(editName))) {
            btnSubmit.setSelected(false);
        } else {
            if (UI.toString(editName).length() >= 6)
                btnSubmit.setSelected(true);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editName))) {
                    ToastUtil.makeText(this, "邀请码不能为空");
                    return;
                }
                getInviteCheck(UI.toString(editName));
                break;
        }
    }
}
